options compress=YES;

libname original ''; 
libname fra '';

%let dir = ;
libname dir '' ; 

* PREP EARNINGS ;

* Unzip files ;
%macro unzip(dsn);
data _null_;
       infile inzip
        lrecl=256 recfm=F length=length eof=eof unbuf;
       file   ds lrecl=256 recfm=N;
       input;
       put _infile_ $varying256. length;
       return;
     eof:
       stop;
    run;
%mend unzip;


%macro mbrphus(seg);

data fra.mbrphusser&seg.;
    set work.sum&seg.sas
	(
	keep = ssn sex race fst_yr_earnings lst_yr_earnings dob_: dod_: 
	total_qc_4750 total_qc_5177 total_earnings_3750 total_earnings_5177 total_earnings_78td
	sum_earns_1-sum_earns_64
	);

	* Create one variable for DOB and DOD;
	ser_dob=mdy(dob_mo,1,cats(dob_cc,dob_yr));
	ser_dod=mdy(dod_mo,1,cats(dod_cc,dod_yr));
	format ser_dob date9.;
	format ser_dod date9.;
	drop dob_: dod_:;

	rename sex=ser_sex race=ser_race;

	* Rename earnings variables ;
	%macro rename(year,no);
		rename sum_earns_&no.=sum_earns_&year.;
	%mend rename;

	%rename(1951,1);
	%rename(1952,2);
	%rename(1953,3);
	%rename(1954,4);
	%rename(1955,5);
	%rename(1956,6);
	%rename(1957,7);
	%rename(1958,8);
	%rename(1959,9);
	%rename(1960,10);
	%rename(1961,11);
	%rename(1962,12);
	%rename(1963,13);
	%rename(1964,14);
	%rename(1965,15);
	%rename(1966,16);
	%rename(1967,17);
	%rename(1968,18);
	%rename(1969,19);
	%rename(1970,20);
	%rename(1971,21);
	%rename(1972,22);
	%rename(1973,23);
	%rename(1974,24);
	%rename(1975,25);
	%rename(1976,26);
	%rename(1977,27);
	%rename(1978,28);
	%rename(1979,29);
	%rename(1980,30);
	%rename(1981,31);
	%rename(1982,32);
	%rename(1983,33);
	%rename(1984,34);
	%rename(1985,35);
	%rename(1986,36);
	%rename(1987,37);
	%rename(1988,38);
	%rename(1989,39);
	%rename(1990,40);
	%rename(1991,41);
	%rename(1992,42);
	%rename(1993,43);
	%rename(1994,44);
	%rename(1995,45);
	%rename(1996,46);
	%rename(1997,47);
	%rename(1998,48);
	%rename(1999,49);
	%rename(2000,50);
	%rename(2001,51);
	%rename(2002,52);
	%rename(2003,53);
	%rename(2004,54);
	%rename(2005,55);
	%rename(2006,56);
	%rename(2007,57);
	%rename(2008,58);
	%rename(2009,59);
	%rename(2010,60);
	%rename(2011,61);
	%rename(2012,62);
	%rename(2013,63);
	%rename(2014,64);
	;
run;

* INFLATE EARNINGS;
data fra.mbrphusser&seg.;
	set fra.mbrphusser&seg.;

	* Inflate earnings to 2017 dollars;
	n_cpi_1951=9.56;
	n_cpi_1952=9.16;
	n_cpi_1953=9.13;
	n_cpi_1954=9.20;
	n_cpi_1955=9.10;
	n_cpi_1956=9.06;
	n_cpi_1957=8.80;
	n_cpi_1958=8.49;
	n_cpi_1959=8.37;
	n_cpi_1960=8.29;
	n_cpi_1961=8.15;
	n_cpi_1962=8.09;
	n_cpi_1963=7.99;
	n_cpi_1964=7.86;
	n_cpi_1965=7.78;
	n_cpi_1966=7.64;
	n_cpi_1967=7.38;
	n_cpi_1968=7.12;
	n_cpi_1969=6.82;
	n_cpi_1970=6.42;
	n_cpi_1971=6.10;
	n_cpi_1972=5.91;
	n_cpi_1973=5.70;
	n_cpi_1974=5.21;
	n_cpi_1975=4.66;
	n_cpi_1976=4.37;
	n_cpi_1977=4.15;
	n_cpi_1978=3.89;
	n_cpi_1979=3.56;
	n_cpi_1980=3.12;
	n_cpi_1981=2.79;
	n_cpi_1982=2.58;
	n_cpi_1983=2.48;
	n_cpi_1984=2.38;
	n_cpi_1985=2.30;
	n_cpi_1986=2.22;
	n_cpi_1987=2.18;
	n_cpi_1988=2.10;
	n_cpi_1989=2.01;
	n_cpi_1990=1.91;
	n_cpi_1991=1.80;
	n_cpi_1992=1.76;
	n_cpi_1993=1.70;
	n_cpi_1994=1.66;
	n_cpi_1995=1.62;
	n_cpi_1996=1.57;
	n_cpi_1997=1.53;
	n_cpi_1998=1.50;
	n_cpi_1999=1.48;
	n_cpi_2000=1.44;
	n_cpi_2001=1.39;
	n_cpi_2002=1.37;
	n_cpi_2003=1.34;
	n_cpi_2004=1.31;
	n_cpi_2005=1.27;
	n_cpi_2006=1.22;
	n_cpi_2007=1.20;
	n_cpi_2008=1.15;
	n_cpi_2009=1.15;
	n_cpi_2010=1.12;
	n_cpi_2011=1.10;
	n_cpi_2012=1.07;
	n_cpi_2013=1.05;
	n_cpi_2014=1.04;

	array cpi(1951:2014) n_cpi_1951-n_cpi_2014;
	array earn(1951:2014) sum_earns_1951-sum_earns_2014;
	array earnyes(1951:2014) n_earnyes_1951-n_earnyes_2014;

	do i = 1951 to 2014;
		earn(i) = earn(i)*cpi(i);
	end;

	drop n_cpi_:;

	* Count the number of years of earnings before age 60;
	do i= 1951 to 2014;
		if earn(i)>0 and i<year(ser_dob)+60 then earnyes(i)=1;
			else earnyes(i)=0;
	end;

	n_earnyrnum60=sum(of n_earnyes_:);

	* Average earnings ages 25-55 ;
	n_earnavg25to55=0;
	do i= 1951 to 2014;
		if (i>=year(ser_dob)+25 & i<year(ser_dob)+55) 
			then n_earnavg25to55=n_earnavg25to55+earn(i);
	end;
	n_earnavg25to55=n_earnavg25to55/30;

	* Avg ann earnings decline between ages 55 and 60;
	array temp_earn(55:60) temp_earn55-temp_earn60;
	array temp_earndelt(56:60) temp_earndelt56-temp_earndelt60;

	do i= 55 to 60;
		temp_earn(i)=0;
	end;

	do i= 1951 to 2014;
		if i=year(ser_dob)+55 then temp_earn55=earn(i);
		if i=year(ser_dob)+56 then temp_earn56=earn(i);
		if i=year(ser_dob)+57 then temp_earn57=earn(i);
		if i=year(ser_dob)+58 then temp_earn58=earn(i);
		if i=year(ser_dob)+59 then temp_earn59=earn(i);
		if i=year(ser_dob)+60 then temp_earn60=earn(i);
	end;

	do i= 56 to 60;
	if temp_earn(i-1)^=0 
			then temp_earndelt(i)=(temp_earn(i)-temp_earn(i-1))/temp_earn(i-1);
		else if temp_earn(i-1)=0 & temp_earn(i)^=0
			then temp_earndelt(i)=(temp_earn(i)-temp_earn(i-1))/((temp_earn(i-1)+temp_earn(i))/2);
		else if temp_earn(i-1)=0 & temp_earn(i)=0
			then temp_earndelt(i)=0;
	end;

	n_earndelt55to60=mean(temp_earndelt56, temp_earndelt57, temp_earndelt58, temp_earndelt59, temp_earndelt60);

	* Indicator for whether working at age 55 ;
	n_earnat55=0;
	do i= 1951 to 2014;
		if earn(i)>0 and i=year(ser_dob)+55 then n_earnat55=1;
	end;

	* Define retirement as the latest year in which person had no earnings this year 
	but had earnings last year, intentionally replacing the value as we loop ;
	n_retire_yr=.;
	do i = 1952 to 2014;
		if earn(i)=0 and earn(i-1)^=0 then n_retire_yr=i-1;
	end;

	* Robustness to using first year of retirement ;
	n_retire_yr_1=.;
	temp_1 = . ;
	do i = 1952 to 2014;
		if earn(i)=0 and earn(i-1)^=0 & (year(ser_dob) + 55 <= i) & temp_1 = . then do ;
			temp_1 = i-1 ;
			n_retire_yr_1=i-1;
		end ;
	end;

	* Robustness to multiple years of with no earnings ;
	
	n_retire_yr_rbst=.;
	do i = 1952 to 2012;
		if earn(i)=0 & earn(i-1)^=0 & earn(i+1)=0 & earn(i+2)=0
			then n_retire_yr_rbst=i-1;
	end;

	* Retirement flow variable ;
	array rflow(1951:2013) n_rflow_1951-n_rflow_2013;
	do i = 1952 to 2014;
		if earn(i)=0 and earn(i-1)^=0 then rflow(i-1)=1;
			else rflow(i-1)=0;
	end;

	* How many "flow" retirement events (after 55) does each person have? ;
	restat_num_rflow = 0 ;
	do i = 1951 to 2013;
		if rflow(i)=1 & (year(ser_dob) + 55 <= i) 
			then restat_num_rflow = restat_num_rflow + 1 ;
	end;

	* Hazard definition of retirement (last and first) ;
	array hzlast(1951:2013) n_hzlast_1951-n_hzlast_2013;
	do i = 1951 to 2013;
		if n_retire_yr=i then hzlast(i)=1 ;
		else if n_retire_yr>i then hzlast(i)=0 ;
		else if n_retire_yr<i then hzlast(i)=. ;
	end ;

	array hzfirst(1951:2013) n_hzfirst_1951-n_hzfirst_2013;
	temp_hz = . ;
      do i = 1952 to 2014;
            if earn(i)=0 and earn(i-1)^=0 & (year(ser_dob) + 55 <= i) & temp_hz = . then do ;
				temp_hz = i-1 ;
				hzfirst(i-1)=1;
			end ;
			else hzfirst(i-1) = 0 ;
      end;

	* Earnings indicator for a particular year;
	array anyearn(1951:2014) n_anyearn_1951-n_anyearn_2014;
	do i= 1951 to 2014;
		if earn(i)>0 then anyearn(i)=1;
			else anyearn(i)=0;
	end;

	drop i sum_earns_: n_earnyes_: temp_: ;

run;

* Select only those with YOB between 1939 and 1959 and who have at least 10 years of earnings ;
data fra.mbrphusser&seg.;
	merge

	fra.mbrphusser&seg.
	(where=(year(ser_dob)>=1935 & year(ser_dob)<=1959)
	in=in_serx)

	fra.mbrphus&seg._primary
	(in=in_mbrx rename=(can=ssn)
	keep = can bic dobp_ccyy sexprimary race doei_: doec_: n_:)
	; 

	by ssn;
	if in_serx=1;
	in_mbr=in_mbrx;
run;

* Create collapse variable and value for those who have not retired/claimed yet;
* Create a variable that is the difference between retirement and claiming (stickiness);
data fra.mbrphusser&seg.;
	retain ssn ser_dob dobp_ccyy ser_sex sexprimary ser_race race 
		n_retire_yr n_retire_yr_1 n_retire_yr_rbst n_phus1any_yr n_phus1any_mo n_phus1old_yr n_phus1old_mo 
		n_mba1any_yr n_mba1any_mo n_mba1old_yr n_mba1old_mo 
		n_flag_phus n_flag_mba n_flag_phusmba in_mbr
		doei_bic doei_tob doei_mm doei_ccyy
		doec_bic doec_tob doec_mm doec_ccyy
		;
	set fra.mbrphusser&seg.;

	ser_dobyy=year(ser_dob);
	ser_dobmm=month(ser_dob);
	ser_dobym=year(ser_dob)+month(ser_dob)/12;

	n_retire_age = 		n_retire_yr - ser_dobyy ;
	n_retire_age_1 =	n_retire_yr_1 - ser_dobyy ;
	n_retire_age_rbst = n_retire_yr_rbst - ser_dobyy ;

	n_claim_age_phus =	n_phus1any_yr - ser_dobyy ;
	n_claim_age_mba =	n_mba1any_yr - ser_dobyy ;

	%macro age(var,age1,age2) ;
		if 		&var.  >= &age1. & 	&var.  <= &age2.	then &var.&age1.&age2. = 1 ; 
		else if &var. ^= . 								then &var.&age1.&age2. = 0 ;
		else if &var.  = . 								then &var.&age1.&age2. = . ;
	%mend age ;

	%age(n_retire_age,50,61) ; %age(n_retire_age,62,62) ; 	%age(n_retire_age,63,64) ;  
	%age(n_retire_age,65,65) ; 	%age(n_retire_age,66,66) ;  %age(n_retire_age,67,80) ;

	%age(n_retire_age_1,50,61) ; %age(n_retire_age_1,62,62) ; 	%age(n_retire_age_1,63,64) ;  
	%age(n_retire_age_1,65,65) ; 	%age(n_retire_age_1,66,66) ;  %age(n_retire_age_1,67,80) ;

	%age(n_retire_age_rbst,50,61) ; %age(n_retire_age_rbst,62,62) ; 	%age(n_retire_age_rbst,63,64) ;  
	%age(n_retire_age_rbst,65,65) ; 	%age(n_retire_age_rbst,66,66) ;  %age(n_retire_age_rbst,67,80) ;

	%age(n_claim_age_phus,50,61) ; %age(n_claim_age_phus,62,62) ; 	%age(n_claim_age_phus,63,64) ;  
	%age(n_claim_age_phus,65,65) ; 	%age(n_claim_age_phus,66,66) ;  %age(n_claim_age_phus,67,80) ;

	%age(n_claim_age_mba,50,61) ; %age(n_claim_age_mba,62,62) ; 	%age(n_claim_age_mba,63,64) ;  
	%age(n_claim_age_mba,65,65) ; 	%age(n_claim_age_mba,66,66) ;  %age(n_claim_age_mba,67,80) ;

	n_diff_phusanyret=	n_phus1any_yr-n_retire_yr;
	n_diff_phusoldret=	n_phus1old_yr-n_retire_yr;
	n_diff_mbaanyret=	n_mba1any_yr-n_retire_yr;
	n_diff_mbaoldret=	n_mba1old_yr-n_retire_yr;

	n_diff_phusanyret_rbst=	n_phus1any_yr-n_retire_yr_rbst;
	n_diff_phusoldret_rbst=	n_phus1old_yr-n_retire_yr_rbst;
	n_diff_mbaanyret_rbst=	n_mba1any_yr-n_retire_yr_rbst;
	n_diff_mbaoldret_rbst=	n_mba1old_yr-n_retire_yr_rbst;

	if n_retire_yr=. then n_retire_yr=9999;
	if n_retire_yr_1=. then n_retire_yr_1=9999;
	if n_retire_yr_rbst=. then n_retire_yr_rbst=9999;
	if n_phus1old_yr=. then n_phus1old_yr=9999;
	if n_phus1any_yr=. then n_phus1any_yr=9999;
	if n_mba1old_yr=. then n_mba1old_yr=9999;
	if n_mba1any_yr=. then n_mba1any_yr=9999;
run;
%mend mbrphus;

* Seg 02 ;
filename inzip ZIP "&dir.sum02sas.zip"
                          member = "sum02sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum02sas.sas7bdat" ;
%unzip(work.sum02sas);
%mbrphus(02);

* Seg 03 ;
filename inzip ZIP "&dir.sum03sas.zip"
                          member = "sum03sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum03sas.sas7bdat" ;
%unzip(work.sum03sas);
%mbrphus(03);

* Seg 05 ;
filename inzip ZIP "&dir.sum05sas.zip"
                          member = "sum05sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum05sas.sas7bdat" ;
%unzip(work.sum05sas);
%mbrphus(05);

* Seg 06 ;
filename inzip ZIP "&dir.sum06sas.zip"
                          member = "sum06sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum06sas.sas7bdat" ;
%unzip(work.sum06sas);
%mbrphus(06);

* Seg 09 ;
filename inzip ZIP "&dir.sum09sas.zip"
                          member = "sum09sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum09sas.sas7bdat" ;
%unzip(work.sum09sas);
%mbrphus(09);

* Seg 10 ;
filename inzip ZIP "&dir.sum10sas.zip"
                          member = "sum10sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum10sas.sas7bdat" ;
%unzip(work.sum10sas);
%mbrphus(10);

* Seg 15 ;
filename inzip ZIP "&dir.sum15sas.zip"
                          member = "sum15sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum15sas.sas7bdat" ;
%unzip(work.sum15sas);
%mbrphus(15);

* Seg 16 ;
filename inzip ZIP "&dir.sum16sas.zip"
                          member = "sum16sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum16sas.sas7bdat" ;
%unzip(work.sum16sas);
%mbrphus(16);

* Seg 19 ;
filename inzip ZIP "&dir.sum19sas.zip"
                          member = "sum19sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum19sas.sas7bdat" ;
%unzip(work.sum19sas);
%mbrphus(19);

* Seg 20 ;
filename inzip ZIP "&dir.sum20sas.zip"
                          member = "sum20sas.sas7bdat";
filename ds "%sysfunc(getoption(work))\sum20sas.sas7bdat" ;
%unzip(work.sum20sas);
%mbrphus(20);

run;
quit;


